package stock;

import java.util.List;

import org.hibernate.Session;
import org.springframework.orm.hibernate5.HibernateTransactionManager;

import common.Logger;
import net.sf.json.JSONObject;

public class StockRunnable implements Runnable{

	private List list;
	private int start;
	private int limit;
	public static int count = 0;
	
	private List<JSONObject> listjson;
	public static Logger logger = Logger.getLogger(Object.class);

	public StockRunnable(List list, int start, int limit,List<JSONObject> listjson) {

		this.list = list;
		this.start = start;
		this.limit = limit;
		this.listjson = listjson;
	}

	@Override
	public void run() {
		List<String> listCode = (List<String>) list;  
		Session session = ContextUtil.getSessionFactory1().openSession();
		for (int m = start; m < limit; m++) {
			session.beginTransaction();
			String sql1 = " from Stock t where  t.code = ?0 ORDER BY t.date desc ";
			String sql2 = " select count(*) from Stock t where  t.code = ?0 and t.zhandifu >=5   ORDER BY t.date desc ";
			String sql3 = " select count(*) from Stock t where  t.code = ?0 and t.zhandifu <= -5  ORDER BY t.date desc ";
			String sql4 =  "select count(*),max(t.date),min(t.date) from stock t where t.`code` = ?0";
			String sqlName = "from Stock t where t.code = ?0 ";
            int sum = 0; 
        	 StockSelectImpl.flagA++;
        	 String code = listCode.get(m);
 			System.out.println("--------正在计算--" + code + "-------------------"+StockSelectImpl.flagA ); 
 			List<Stock> list2 = session.createQuery(sql1).setParameter(0, code).setFirstResult(0).setMaxResults(StockSelectImpl.zhang)
 					.list(); 
 			Float f = 5F; 
 			Object zhang5 = (Object) session.createSQLQuery(sql2).setParameter(0, code).uniqueResult();
 			Object die5 = (Object) session.createSQLQuery(sql3).setParameter(0, code).uniqueResult();
 			Stock stock = (Stock) session.createQuery(sqlName).setParameter(0, code).setFirstResult(0).setMaxResults(1).list().get(0);
 			List<Object[]> listk  = session.createSQLQuery(sql4).setParameter(0, code).list();
 			boolean b = false;
 			float sum1 = 0f;
 			for (int j = 0; j < list2.size(); j++) {
 				Stock sc = list2.get(j);
 				sum1 += sc.getZhandifu();
 				if (sc.getZhandifu() < 0) {
 					b = true;
 					break;
 				}
 				if (sc.getChengjiaoliang() <= 0) {
 					b = true;
 					break;
 				}
 				if (sc.getName().indexOf("ST") >= 0 || sc.getName().indexOf("退") >= 0) {
 					b = true;
 					break;
 				} 
 			}
 			if (b == false) {
 				JSONObject jsonObject = new JSONObject();
 				jsonObject.put("code", code);
 				jsonObject.put("sum", sum1);
 				jsonObject.put("zhang5", zhang5);
 				jsonObject.put("die5", die5);
 				jsonObject.put("stock", stock.getName());
 				jsonObject.put("diqv", stock.getDiqv());
 				Object[] obj  = listk.get(0);
 				jsonObject.put("maxDate", obj[0]);
 				jsonObject.put("minDate", obj[2]);
 				jsonObject.put("count", obj[1]);
 				jsonObject.put("cDate", DateTools.getCurrentDate(""));
 				listjson.add(jsonObject);
 			}
 			System.out.println("--------符合要求--" + listjson.size() + "-------------------"); 
     	//--
			session.getTransaction().commit(); 
		}
		session.clear();
		session.close(); 
	}

}
